home *** CD-ROM | disk | FTP | other *** search
- #include "Ticket to Program.h"
-
-
- /************** Main ********************/
- /* This is main section of the program */
- /****************************************/
- main()
- {
- ToolBoxInit();
- if (Sys60rLater() )
- {
- DialogInit();
- MenuBarInit ();
- SetUpDragRect ();
- MainLoop ();
- }
- }
-
-
- /****************** ToolBoxInit **************************/
- /* Initialize all of the commonly used toolbox routines */
- /*********************************************************/
- ToolBoxInit()
- {
- InitGraf(&thePort);
- InitFonts();
- FlushEvents(everyEvent, REMOVE_ALL_EVENTS);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NIL_POINTER);
- InitCursor();
- }
-
-
- /****************** Sys60rLater *********************/
- /* Check version of system running to determine */
- /* if we should use GetNextEvent or WaitNextEvent */
- /****************************************************/
- Sys60rLater ()
- {
- OSErr status;
- SysEnvRec SysEnvData;
-
- status = SysEnvirons (SYS_VERSION, &SysEnvData);
- if (( status != noErr) || (SysEnvData.systemVersion < 0x0600) )
- {
- StopAlert (BAD_SYS_ALERT, NIL_POINTER);
- return (FALSE);
- }
- else
- return (TRUE);
- }
-
-
- /******************* DialogInit *****************************/
- /* This reads in the dialog window for the add menu item */
- /************************************************************/
- DialogInit ()
- {
- gSettingsDialog = GetNewDialog (BASE_RES_ID, NIL_POINTER, MOVE_TO_FRONT);
- }
-
-
- /******************** SetUpDragRect *******************/
- /* This sets up the drag routine for windows to */
- /* omit the scroll bars. */
- /******************************************************/
- SetUpDragRect()
- {
- gDragRect = screenBits.bounds;
- gDragRect.left += DRAG_THRESHOLD;
- gDragRect.right -= DRAG_THRESHOLD;
- gDragRect.bottom -= DRAG_THRESHOLD;
- }
-
-
- /**************** MenuBarInit ********************/
- /* This routine reads in the menu bar from the */
- /* resource file and displays it. */
- /*************************************************/
- MenuBarInit ()
- {
- Handle myMenuBar;
-
- myMenuBar = GetNewMBar(BASE_RES_ID);
- SetMenuBar(myMenuBar);
- gAppleMenu = GetMHandle(APPLE_MENU_ID);
- AddResMenu (gAppleMenu, 'DRVR');
-
- gFileMenu = GetMHandle(FILE_MENU_ID);
- gEditMenu = GetMHandle(EDIT_MENU_ID);
-
- gMenusMenu = GetMHandle(MENUS_MENU_ID);
- gFontMenu = GetMenu (FONT_MENU_ID);
- InsertMenu (gFontMenu, -1);
- AddResMenu (gFontMenu, 'FONT');
-
- gAlertMenu = GetMHandle (ALERT_MENU_ID);
-
- gWindowMenu = GetMHandle(WINDOW_MENU_ID);
-
- DrawMenuBar();
- }
-
-
- /************** MainLoop ****************/
- /* This routine loops handling events */
- /* until the user selects quit. */
- /****************************************/
- MainLoop ()
- {
- gDone = FALSE;
-
-
- while (gDone == FALSE)
- {
- HandleEvent ();
- }
- }
-
-
- /************** HandleEvent ********************/
- /* This routine handles the parsing of events */
- /***********************************************/
- HandleEvent ()
- {
- char theChar;
- GrafPtr oldPort;
-
- WaitNextEvent (everyEvent, &gTheEvent, MIN_SLEEP, NIL_MOUSE_REGION);
-
- switch (gTheEvent.what)
- {
- case mouseDown:
- HandleMouseDown();
- break;
-
- case updateEvt:
- if ( ! IsDAWindow (gTheEvent.message) )
- {
- GetPort(&oldPort);
- SetPort(gTheEvent.message);
- BeginUpdate (gTheEvent.message);
- if (GetWRefCon (FrontWindow () ) == 10)
- {
- DrawPopUp ();
- }
- EndUpdate (gTheEvent.message);
- SetPort (oldPort);
- }
- break;
-
- case keyDown:
- case autoKey:
- theChar = gTheEvent.message & charCodeMask;
- if (( gTheEvent.modifiers & cmdKey ) != 0)
- HandleMenuChoice (MenuKey(theChar));
- break;
- }
- }
-
-
-
- /******************* HandleMouseDown *********************/
- /* This routine handles the parsing of mouseDown events */
- /*********************************************************/
- HandleMouseDown ()
- {
- WindowPtr whichWindow;
- short int thePart, i;
- long int menuChoice, theChoice;
- Point myPoint, popUpUpperLeft;
- GrafPtr oldPort;
-
- thePart = FindWindow (gTheEvent.where, &whichWindow);
- switch (thePart)
- {
- case inMenuBar:
- menuChoice = MenuSelect(gTheEvent.where);
- HandleMenuChoice (menuChoice);
- break;
-
- case inSysWindow:
- SystemClick (&gTheEvent, whichWindow);
- break;
-
- case inDrag:
- DragWindow (whichWindow, gTheEvent.where, &gDragRect);
- break;
-
- case inContent:
- SelectWindow (whichWindow);
- GetPort(&oldPort);
- SetPort(whichWindow);
-
- myPoint = gTheEvent.where;
- GlobalToLocal (&myPoint);
- if (PtInRect (myPoint, &gPopUpRect) )
- {
- InvertRect (&gLabelRect);
- popUpUpperLeft.v = gPopUpRect.top + PIXEL_FOR_TOP_LINE;
- popUpUpperLeft.h = gPopUpRect.left;
- LocalToGlobal (&popUpUpperLeft);
- theChoice = PopUpMenuSelect(gPopUpMenu,
- popUpUpperLeft.v, popUpUpperLeft.h, gPopUpItem);
- InvertRect (&gLabelRect);
- if (LoWord (theChoice) > 0)
- {
- gPopUpItem = LoWord(theChoice);
- DrawPopUpNumber();
- for (i=0; i<gPopUpItem; i++)
- SysBeep(20);
- }
- SetPort (oldPort);
- }
- break;
-
- case inGoAway:
- if (TrackGoAway(whichWindow, gTheEvent.where))
- DisposeWindow(whichWindow);
- break;
- }
- }
-
-
- /*************** IsDAWindow ******************/
- /* This routine checks to see if the window */
- /* that is selected is a DA window or not. */
- /*********************************************/
- IsDAWindow (whichWindow)
- WindowPtr whichWindow;
- {
- if (whichWindow == NIL_POINTER)
- return (FALSE);
- else
- return ( ( (WindowPeek)whichWindow)->windowKind<0);
- }
-
-
- /************* HandleMenuChoice ************/
- /* This routine handles parsing of events */
- /* when the user clicks in the menu bar. */
- /*******************************************/
- HandleMenuChoice (menuChoice)
- long int menuChoice;
- {
- int theMenu;
- int theItem;
-
- if (menuChoice != 0)
- {
- theMenu = HiWord (menuChoice);
- theItem = LoWord (menuChoice);
- switch (theMenu)
- {
- case APPLE_MENU_ID:
- HandleAppleChoice (theItem);
- break;
-
- case FILE_MENU_ID:
- HandleFileChoice (theItem);
- break;
-
- case EDIT_MENU_ID:
- HandleEditChoice (theItem);
- break;
-
- case MENUS_MENU_ID:
- HandleMenusChoice (theItem);
- break;
-
- case ALERT_MENU_ID:
- HandleAlertChoice (theItem);
- break;
-
- case WINDOW_MENU_ID:
- HandleWindowChoice (theItem);
- break;
- }
-
- }
- HiliteMenu (0);
- }
-
-
-
- /************ HandleAppleChoice *************/
- /* This routine handles parsing of events */
- /* when the user clicks in the Apple menu. */
- /********************************************/
- HandleAppleChoice (theItem)
- int theItem;
- {
- Str255 accName;
- int accNumber;
-
- switch (theItem)
- {
- case ABOUT_ITEM_ID:
- NoteAlert ( ABOUT_ALERT, NIL_POINTER);
- break;
-
- default:
- GetItem (gAppleMenu, theItem, accName);
- accNumber = OpenDeskAcc (accName);
- break;
- }
- }
-
-
- /************** HandleFileChoice ************/
- /* This routine handles parsing of events */
- /* when the user clicks in the File menu. */
- /********************************************/
- HandleFileChoice (theItem)
- int theItem;
- {
- switch (theItem)
- {
- case QUIT_ITEM:
-
- gDone = TRUE;
- break;
- }
- }
-
-
- /************ HandleEditChoice **************/
- /* This routine handles parsing of events */
- /* when the user clicks in the Edit menu. */
- /********************************************/
- HandleEditChoice (theItem)
- int theItem;
- {
- switch (theItem)
- {
- SysBeep(12);
- }
- }
-
-
- /************ HandleMenusChoice *************/
- /* This routine handles parsing of events */
- /* when the user clicks in the Menus menu. */
- /********************************************/
- HandleMenusChoice (theItem)
- int theItem;
- {
- int itemType;
- Rect itemRect;
- Handle itemHandle;
-
-
- switch (theItem)
- {
- case ADD_MENU_ITEM:
- HandleDialog();
- GetDItem (gSettingsDialog, TIME_FIELD, &itemType,
- &itemHandle, &itemRect );
- GetIText (itemHandle, &(savedSettings.timeString) );
- AppendMenu (gMenusMenu, &(savedSettings.timeString) );
- gCount = gCount + 1;
- if ( gCount > 2)
- {
- DisableItem (gMenusMenu, ADD_MENU_ITEM);
- break;
- }
- break;
-
- case CHECK_MENU_ITEM:
- gCheck = !gCheck;
- CheckItem (gMenusMenu, CHECK_MENU_ITEM, gCheck);
- break;
-
- case TOGGLE_MENU_ITEM:
- if ( !gToggle )
- {
- DisableItem (gMenusMenu, TOGGLE_MENU_ITEM);
- SetItem (gMenusMenu, TOGGLE_MENU_ITEM, "\pToggle Off");
- EnableItem (gMenusMenu, TOGGLE_MENU_ITEM);
- gToggle = !gToggle;
- break;
- }
- else
- {
- DisableItem (gMenusMenu, TOGGLE_MENU_ITEM);
- SetItem (gMenusMenu, TOGGLE_MENU_ITEM, "\pToggle On");
- EnableItem (gMenusMenu, TOGGLE_MENU_ITEM);
- gToggle = !gToggle;
- break;
- }
-
- case SET_STYLE_ITEM:
- if ( !gStyle )
- {
- SetItemStyle (gMenusMenu, SET_STYLE_ITEM, gCurrentStyle == PLAIN);
- TextFace (gCurrentStyle);
- gStyle = !gStyle;
- break;
- }
- else
- {
- SetItemStyle (gMenusMenu, SET_STYLE_ITEM, gCurrentStyle & bold);
- TextFace (gCurrentStyle);
- gStyle = !gStyle;
- break;
- }
-
- case HIERARCHICAL_ITEM:
- SysBeep(12);
- break;
-
- case POP_UP_ITEM:
- WindowInit ();
- PopUpMenuBar ();
- DrawPopUp ();
- break;
-
- }
- }
-
-
- /************* HandleAlertChoice ************/
- /* This routine handles parsing of events */
- /* when the user clicks in the Alert menu. */
- /********************************************/
- HandleAlertChoice (theItem)
- int theItem;
- {
- switch (theItem)
- {
- case STOP:
- StopAlert (STOP_ALERT, NIL_POINTER);
- break;
-
- case NOTE:
- NoteAlert (NOTE_ALERT, NIL_POINTER);
- break;
-
- case CAUTION:
- CautionAlert (CAUTION_ALERT, NIL_POINTER);
- break;
- }
- }
-
-
- /************* HandleWindowChoice ************/
- /* This routine handles parsing of events */
- /* when the user clicks in the Window menu. */
- /********************************************/
- HandleWindowChoice (theItem)
- int theItem;
- {
- WindowPtr myWindow;
- short windowID;
-
- switch (theItem)
- {
- case document:
- windowID = 401;
- myWindow = GetNewWindow (windowID, nil, -1);
- SetPort (myWindow);
- ShowWindow (myWindow);
- break;
-
- case noGrow:
- windowID = 402;
- myWindow = GetNewWindow (windowID, nil, -1);
- SetPort (myWindow);
- ShowWindow (myWindow);
- break;
-
- case zoomDoc:
- windowID = 403;
- myWindow = GetNewWindow (windowID, nil, -1);
- DrawGrowIcon (myWindow);
- SetPort (myWindow);
- ShowWindow (myWindow);
- break;
-
- case zNoGrow:
- windowID = 404;
- myWindow = GetNewWindow (windowID, nil, -1);
- SetPort (myWindow);
- ShowWindow (myWindow);
- break;
-
- case docProc:
- windowID = 405;
- myWindow = GetNewWindow (windowID, nil, -1);
- SetPort (myWindow);
- ShowWindow (myWindow);
- break;
-
-
- }
- }
-
-
- /******************** WindowInit ********************/
- /* This routine reads in froma resource the window */
- /* that will be used to contain the PopUp item */
- /***************************************************/
- WindowInit()
- {
- long value = 10;
-
- gPopUpWindow = GetNewWindow(BASE_RES_ID, NIL_POINTER, MOVE_TO_FRONT);
- ShowWindow (gPopUpWindow);
- SetWRefCon (gPopUpWindow, value);
- SetPort(gPopUpWindow);
- TextFont (systemFont);
- TextMode (srcCopy);
- }
-
-
- /************* PopUpMenuBar **********/
- /* This inserts the PopUp menu bar */
- /* which will be drawn in the window */
- /**************************************/
- PopUpMenuBar ()
- {
- gPopUpMenu = GetMenu(POP_UP_MENU_ID);
- InsertMenu (gPopUpMenu, -1);
- HLock (gPopUpMenu);
- gPopUpLabelWidth = StringWidth( (**gPopUpMenu).menuData);
- HUnlock (gPopUpMenu);
- }
-
-
- /************* DrawPopUp **************/
- /* This routine draws the item to be */
- /* PopUp'ed in the window */
- /***************************************/
- DrawPopUp ()
- {
- SetRect (&gPopUpRect, POPUP_LEFT, POPUP_TOP, POPUP_RIGHT, POPUP_BOTTOM);
- FrameRect (&gPopUpRect);
-
- MoveTo (gPopUpRect.left+SHADOW_PIXELS, gPopUpRect.bottom);
- LineTo (gPopUpRect.right, gPopUpRect.bottom);
- LineTo (gPopUpRect.right, gPopUpRect.top+SHADOW_PIXELS);
-
- MoveTo (gPopUpRect.left - gPopUpLabelWidth - RIGHT_MARGIN,
- gPopUpRect.bottom - BOTTOM_MARGIN);
- HLock(gPopUpMenu);
- DrawString( (**gPopUpMenu).menuData);
- HUnlock (gPopUpMenu);
-
- gLabelRect.top = gPopUpRect.top + PIXEL_FOR_TOP_LINE;
- gLabelRect.left = gPopUpRect.left - gPopUpLabelWidth -
- LEFT_MARGIN - RIGHT_MARGIN;
- gLabelRect.right = gPopUpRect.left;
- gLabelRect.bottom = gPopUpRect.bottom;
-
- DrawPopUpNumber();
- }
-
-
- /************* DrawPopUpNumber *************/
- /* This reads in the items from a resouce */
- /* and displays them in the PopUp menu */
- /*******************************************/
- DrawPopUpNumber ()
- {
- Str255 menuItem;
- int itemLeftMargin;
-
- GetItem (gPopUpMenu, gPopUpItem, &menuItem);
- itemLeftMargin = (gPopUpRect.right - gPopUpRect.left -
- StringWidth(menuItem)) /2;
- MoveTo (gPopUpRect.left + itemLeftMargin,
- gPopUpRect.bottom - BOTTOM_MARGIN);
- DrawString (menuItem);
- }
-
-
- /************** HandleDialog **************/
- /* This routine display a ModalDialog */
- /* when the users clicks on the add */
- /* menu item. */
- /******************************************/
- HandleDialog ()
- {
- int itemHit, dialogDone = FALSE;
-
- ShowWindow (gSettingsDialog);
- /* *(savedSettings.timeString) = nil;*/
- /* SaveSettings ();*/
-
- while (dialogDone == FALSE)
- {
- ModalDialog (NIL_POINTER, &itemHit);
- switch (itemHit)
- {
- case SAVE_BUTTON:
- HideWindow (gSettingsDialog);
- dialogDone = TRUE;
- break;
-
- case CANCEL_BUTTON:
- HideWindow (gSettingsDialog);
- RestoreSettings ();
- dialogDone = TRUE;
- break;
- }
- }
- }
-
-
- /************* SaveSettings ***************/
- /* This routine saves the value the user */
- /* enters into the ModalDialog box when */
- /* they select add menu item. */
- /******************************************/
- SaveSettings ()
- {
- int itemType;
- Rect itemRect;
- Handle itemHandle;
-
- GetDItem (gSettingsDialog, TIME_FIELD, &itemType, &itemHandle, &itemRect );
- GetIText (itemHandle, &(savedSettings.timeString) );
-
- }
-
-
- /************ RestoreSettings **************/
- /* This routine stores the value for the */
- /* get menu item entry if the user selects */
- /* the cancel button. */
- /********************************************/
- RestoreSettings ()
- {
- int itemType;
- Rect itemRect;
- Handle itemHandle;
-
- GetDItem (gSettingsDialog, TIME_FIELD, &itemType, &itemHandle, &itemRect );
- SetIText (itemHandle, savedSettings.timeString);
-
- }